package com.ts.proj.json;

import com.ts.base.controller.BaseController;
import com.ts.common.constant.AuthConstant;
import com.ts.common.constant.ConsoleConstant;
import com.ts.common.constant.SystemUserTableColumnConstant;
import com.ts.proj.entity.SystemRoleEntity;
import com.ts.proj.entity.SystemUserEntity;
import com.ts.proj.service.SystemRoleService;
import com.ts.proj.service.SystemUserService;
import com.ts.util.JsonUtils;
import com.ts.util.PageData;
import com.ts.util.RightsHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@Slf4j
public class IndexJson extends BaseController {

    @Autowired
    private SystemUserService systemUserService;

    @Autowired
    private SystemRoleService systemRoleService;

    /**
     * 重登录
     * @return
     */
    @RequestMapping(value = "/login_tologin/{loginName}/{loginPass}",method = RequestMethod.POST)
    public Map<String,String> login(@PathVariable("loginName") String loginName,@PathVariable("loginPass") String loginPass){
        Map<String, String> resultMap = new HashMap<>();
        PageData pd = new PageData();
        pd.put(SystemUserTableColumnConstant.USERNAME, loginName);
        List<SystemUserEntity> systemUserEntityList = systemUserService.findAll(pd);
        if(null == systemUserEntityList ||0== systemUserEntityList.size()){
            resultMap.put(ConsoleConstant.MSG, AuthConstant.USERNOTEXIST);
            return resultMap;
        }else{
            SystemUserEntity systemUserEntity = systemUserEntityList.get(0);
            String secret = new Md5Hash(loginPass, systemUserEntity.getUsername() + systemUserEntity.getSalt()).toString();
            if(!secret.equals(systemUserEntity.getPassword())){
                resultMap.put(ConsoleConstant.MSG, AuthConstant.USERNAMEORPASSISERROR);
                return resultMap;
            }
            if(!systemUserEntity.getLocked()){
                resultMap.put(ConsoleConstant.MSG, AuthConstant.USERISLOCKED);
                return resultMap;
            }
            String rights = "";
            String btnRights = "";
            //虚拟权限
            String tempRights = "";
            String tempBtnRights = "";
            List<Integer> roleIdList = new ArrayList<>();
            try{
                List<Integer> ids = new ArrayList<>();
                for(int i=0;i<500;i++){
                    if(RightsHelper.testRights(String .valueOf(systemUserEntity.getRoleId()), i)){
                        ids.add(i);
                    }
                }
                List<SystemRoleEntity> allOrOne = systemRoleService.getSystemRoleEntityListByIds(ids);

                //获取权限
                List<String> tempRightsList = new ArrayList<>();
                //获取按钮
                List<String> tempBtnRightsList = new ArrayList<>();

                for(int i=0;i<allOrOne.size();i++){
                    SystemRoleEntity systemRoleEntity = allOrOne.get(i);
                    String rightsSingle = systemRoleEntity.getRights().substring(1);
                    String btnRightsSingle = systemRoleEntity.getBtnRights().substring(1);
                    roleIdList.add(systemRoleEntity.getId());
                    for(int j=0;j<2500;j++){
                        if (RightsHelper.testRights(rightsSingle, j)&&!tempRightsList.contains(String.valueOf(j))) {
                            tempRightsList.add(String.valueOf(j));
                        }
                        if(RightsHelper.testRights(btnRightsSingle,j)&&!btnRightsSingle.contains(String.valueOf(j))){
                            tempBtnRightsList.add(String.valueOf(j));
                        }
                    }

                }

                String [] rightsStrings = tempRightsList.toArray(new String[tempRightsList.size()]);
                String [] btnRightsString = tempBtnRightsList.toArray(new String[tempBtnRightsList.size()]);


                tempRights = "R" + RightsHelper.sumRights(rightsStrings).toString();
                tempBtnRights = "B" + RightsHelper.sumRights(btnRightsString).toString();

                rights = allOrOne.get(0).getRights();
                btnRights = allOrOne.get(0).getBtnRights();
            }catch (Exception e){

            }


            //登陆成功，存入session
            HttpSession session = this.getRequest().getSession();
            //将用户存入session
            session.setAttribute(ConsoleConstant.SESSIONUSER, JsonUtils.objectToJson(systemUserEntity));
            //将页面权限传入session
            session.setAttribute(ConsoleConstant.RIGHTS, tempRights);
            session.setAttribute(ConsoleConstant.REALRIGHTS, rights);
            session.setAttribute(ConsoleConstant.ROLEID, JsonUtils.objectToJson(roleIdList));
            session.setAttribute(ConsoleConstant.BUTTONRIGHTS, tempBtnRights);
            resultMap.put(ConsoleConstant.MSG, AuthConstant.SUCCESS);
            return resultMap;
        }
    }
}
